package controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import pojo.BaseDict;
import pojo.Customer;
import pojo.QueryVo;
import service.BaseDictService;
import service.CustomerService;
import utils.Page;

import java.util.List;

@Controller
@RequestMapping("customer")
public class CustomerController {

    @Autowired
    private BaseDictService baseDictService;

    @Autowired
    private CustomerService customerService;

    @Value("${customer_fromType}")
    private String customer_fromType;

    @Value("${customer_levelType}")
    private String customer_levelType;

    @Value("${customer_industryType}")
    private String customer_industryType;

    @RequestMapping("list")
    public String list(Model model, QueryVo queryVo){

        //查询来源
        List<BaseDict> fromType = baseDictService.getBaseDictByCode(customer_fromType);
        //查询等级
        List<BaseDict> levelType = baseDictService.getBaseDictByCode(customer_levelType);
        //查询来源
        List<BaseDict> industryType = baseDictService.getBaseDictByCode(customer_industryType);

        //设置数据模型返回
        model.addAttribute("fromType",fromType);
        model.addAttribute("levelType",levelType);
        model.addAttribute("industryType",industryType);

        //根据查询条件分页查询用户列表
        Page<Customer> page = customerService.getCustomerByQueryVo(queryVo);

        model.addAttribute("page",page);

        model.addAttribute("vo",queryVo);

        return "customer";
    }

    @RequestMapping("edit")
    @ResponseBody
    public Customer edit(Integer id){
        Customer customer = customerService.getCustomerById(id);
        return customer;
    }

    @RequestMapping("update")
    //如果不加ResponseBody注解，status会被当成字符来解析
    @ResponseBody
    public String update(Customer customer){
        String status = "0";
        try {
            customerService.updateCustomer(customer);
            status = "1";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  status;
    }

    @RequestMapping("delete")
    @ResponseBody
    public String delete(Integer id){
        String status = "0";
        try {
            customerService.deleteCustomer(id);
            status = "1";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  status;
    }

    @RequestMapping("myTag")
    public String myTag(){
        return "MyTag";
    }
}
